package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.rmi.MarshalException;
import java.util.List;
import java.util.UUID;

/**
 * @author hql
 * @date 2019-12-04 10:47
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    private SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 文件上传
     * @param imgFile
     * @return
     */
    @RequestMapping("/upload")
 public Result upload(MultipartFile imgFile){

     boolean flag=false;
     String message=null;
    //imgFile.getOriginalFilename()拿到的是上传的时候的源文件名
    //截取最后一个.拿到他的索引值
    int index = imgFile.getOriginalFilename().lastIndexOf(".");
    //截取子串 拿到  .jpg
    String lastName = imgFile.getOriginalFilename().substring(index - 1);
    //生成随机的UUID拼接能够确保不重复的文件名
    String fileName=UUID.randomUUID()+lastName;

    try {
        //调用奇牛云的工具类来上传图片有两种方式1.是传文件名 2是上传图片的字节码文件 第二个参数为自己指定的文件名
        QiniuUtils.upload2Qiniu(imgFile.getBytes(),fileName);
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);
        flag=true;
        message=MessageConstant.UPLOAD_SUCCESS;
    } catch (IOException e) {
        e.printStackTrace();
        message=MessageConstant.PIC_UPLOAD_FAIL;
    }
    return new Result(flag,message,fileName);
 }


    /**
     * 添加体检套餐及其对应的检查项的信息
     * @param ids
     * @param setmeal
     * @return
     */
    @RequestMapping("/addSetmeal")
    public Result addSetmeal(Integer[] ids, @RequestBody Setmeal setmeal){
        boolean flag=false;
        String message=null;
        try {
            setmealService.addSetmeal(ids,setmeal);
            flag=true;
            message= MessageConstant.QUERY_CHECKITEM_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            message=MessageConstant.QUERY_CHECKITEM_FAIL;
        }

        return new Result(flag,message);
    }

    /**
     * 分页查询体检套餐
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findAllSetmeal")
    public Result findAllSetmeal(@RequestBody QueryPageBean queryPageBean){
        boolean flag=false;
        String message=null;
        PageResult pageResult=null;
        try {
            pageResult=setmealService.findAllSetmeal(queryPageBean);
            flag=true;
            message= MessageConstant.QUERY_SETMEAL_SUCCESS;
        }
        catch (RuntimeException r){
            message=r.getMessage();
        }catch (Exception e) {
            e.printStackTrace();
            message=MessageConstant.QUERY_SETMEAL_FAIL;
        }

        return new Result(flag,message,pageResult);
    }

    /**
     * 根据检查套餐的id回显出对应的数据进行修改
     * @param id
     * @return
     */
    @RequestMapping("/findSetmeal")
    public Result findSetmeal(Integer id){
        boolean flag=false;
        String message=null;
        Setmeal setmeal=null;
        try {
           setmeal= setmealService.findSetmeal(id);
            flag=true;
            message= MessageConstant.QUERY_SETMEAL_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            message=MessageConstant.QUERY_SETMEAL_FAIL;
        }

        return new Result(flag,message,setmeal);
    }

    /**
     * 编辑修改检查套餐的信息
     * @param setmeal 前台提交的检查套餐信息
     * @return
     */
    @RequestMapping("/updateSetmeal")
    public Result updateSetmeal(@RequestBody Setmeal setmeal){
        boolean flag=false;
        String message=null;
        try {
           setmealService.updateSetmeal(setmeal);
            flag=true;
            message= MessageConstant.EDIT_CHECKGROUP_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            message=MessageConstant.EDIT_CHECKITEM_FAIL;
        }

        return new Result(flag,message);
    }

    /**
     * 删除检查套餐的信息
     * @param id
     * @return
     */
    @RequestMapping("/delSetmeal")
    public Result delSetmeal(Integer id){
        boolean flag=false;
        String message=null;
        try {
            setmealService.delSetmeal(id);
            flag=true;
            message= MessageConstant.DELETE_SETMEAL_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            message=MessageConstant.DELETE_SETMEAL_FAIL;
        }

        return new Result(flag,message);
    }
}
